package com.dna.controller;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dna.entity.DepartEntity;
import com.dna.service.DepartService;
import com.dna.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/depart")
@Api("机构信息")
public class DepartController {

    @Autowired
    private DepartService departService;
    
    @GetMapping("list")
    @ApiOperation(value = "查询所有机构")
    public R list() {
        List<DepartEntity> deptlist= departService.queryAll();
        return R.ok().put("data", deptlist);

    }
    

    @PostMapping
    @ApiOperation(value = "保存机构")
    public R saveDepart(DepartEntity dep) {
        DepartEntity DepartEntity = departService.queryOne(dep);
        if (DepartEntity != null) {
            return R.error(dep.getName() + " 已存在!");
        }
        departService.save(dep);

        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新机构")
    public R updateDepart(DepartEntity dep) {
        departService.update(dep);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除机构")
    public R deleteDepart(@RequestBody Set<Long> ids) {
        departService.delete(ids);
        return R.ok();
    }
}